import request from '../../utils/request'
Page({
    data: {
      disabled: true,
      captcha:'',
      maValue:'发送验证码',
      btnstate: "default",
      psIndis:true,
      phone: "",
      password1:"",
      password: "",
      passDisabled:'false',
      passbtnstate:"default",
      nickname:''
    },
    accountInput: function (e) {
      var content = e.detail.value;
      if (content.length === 11) {
        this.setData({ disabled: false, btnstate: "primary", phone: content ,nickname:content});
      }
    },
    pwdBlur1: function (e) {
      var password1 = e.detail.value;
      if (password1 != "") {
        this.setData({ password1: password1 ,psIndis:false});
      }
    },
    pwdBlur: function (e) {
      var password = e.detail.value;
      if (password != "") {
        this.setData({ password: password ,passbtnstate: "primary",passDisabled: false,});
      }
    },
    inpNickname(e){
      this.setData({
        inpNickname:e.detail.value
      })
    },
    maInput(event){
      let captcha=event.detail.value
      this.setData({
        captcha
      })
    },

   //发送验证码
   async sangMa(){
     let that = this;
     let phoneReg = /^1(3|4|5|6|7|8|9)\d{9}$/;
     if(!phoneReg.test(this.data.phone)){
       wx.showToast({
         title: '手机号格式错误',
         icon: 'none'
       })
       return;
     }
     this.setData({
      disabled: true,
      btnstate: "default",
     })

     let downCount = 59;
     //请求验证码
     let res = await request('/captcha/sent',{phone:this.data.phone})
     console.log(res)
     clearInterval(times);
     let times = setInterval(function(){
       
       downCount--;
       that.setData({
        maValue:`重新发送/${downCount}S`
       })
       if(downCount === 0){
        clearInterval(times)
        that.setData({
          maValue:'发送验证码',
          disabled:false,
          btnstate: "primary",
         })
      }
     },1000)
     

   },


    //登录回调
    async login(){
      // 1. 收集表单项数据
      let {phone, password,password1,captcha,nickname} = this.data;
      // 2. 前端验证
      /*
      * 手机号验证：
      *   1. 内容为空
      *   2. 手机号格式不正确
      *   3. 手机号格式正确，验证通过
      * */
      
      if(!phone){
        // 提示用户
        wx.showToast({
          title: '手机号不能为空',
          icon: 'none'
        })
        return;
      }
      // 定义正则表达式

      
      if(!password1){
        wx.showToast({
          title: '密码不能为空',
          icon: 'none'
        })
        return;
      }
      
       if(password !== password1){
        wx.showToast({
          title: '密码不一致',
          icon: 'none'
        })
        return
       }
      
       //验证码验证
       console.log("验证码验证")
       let res = await request('/captcha/verify',{phone,captcha})
       if(res.code !== 200){
           if(res.code === 400){
            wx.showToast({
              title: '请求超出每日限制',
              icon: 'none'
            })
            return
           }
           wx.showToast({
            title: '验证码出错',
            icon: 'none'
          })
        return
       }
       
       //检查手机是否已经注册
      console.log("检擦用户是否已经注册")
      let res2 = await request('/cellphone/existence/check',{phone})
      console.log(res2)
      if(res2.exist===1){
        wx.showToast({
          title: '手机号码已注册',
          icon: 'none'
        })
        // return
      }

      //用户注册
      console.log("用户注册")
      wx.showLoading({
        title: '加载中',
      })
      let result = await request('/register/cellphone',{phone,password,captcha,nickname:phone})
      wx.hideLoading()
      console.log(result)
      if(res.code === 200){
        wx.showToast({
          title: '用户注册成功!',
          duration:2500,
        })
      }else{return}
      wx.reLaunch({
        url: '/pages/login/login'
      })
      

    },
  })